<script setup lang="ts">
import { GetNoteTypeAPI, GetUserSettingAPI, UpdateUserSettingAPI } from '@/services/detail'
import { NoteIndex } from '@/services/notelog'
import { ZiChanIndexAPI } from '@/services/zichan'
import { useMemberStore } from '@/stores'
import type { UserSetting } from '@/types/detail'
import type { Note } from '@/types/notelog'
import type { ZiChanIndexData } from '@/types/zichan'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 获取会员信息
const memberStore = useMemberStore()
const data = ref<Note[]>()
const userSetting = ref<UserSetting>()
const noteTypes = ref<string[]>()
const curNoteType = ref<string>('ALL')
onShow(async () => {
  onRefresherrefresh()
})
//加载初始化
onLoad(async () => {
  await InitNoteType()
  await InitUserSetting()
  await onRefresherrefresh()
})
//初始化用户配置
const InitUserSetting = async () => {
  if (memberStore.profile) {
    const res = await GetUserSettingAPI({ UserName: memberStore.profile.UserName })
    userSetting.value = res.result
  }
}
// 是否触发下拉刷新
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  // 加载数据
  if (memberStore.profile) {
    const res = await NoteIndex({
      Auth: memberStore.profile.Token,
      UserName: '',
      ID: 0,
      UserTitle: '',
      NoteType: curNoteType.value,
      UserGroup: memberStore.profile.UserGroup,
    })
    data.value = res.result
    uni.showToast({ icon: 'success', title: '刷新成功' })
  }
  // 关闭动画
  isTriggered.value = false
}

//返回
const Onleft = () => {
  uni.switchTab({ url: '/pages/my/my' })
}
const Add = () => {
  uni.navigateTo({ url: '/pages/NoteAcount/NoteLog' })
}
//初始化账户类型列表
const InitNoteType = async () => {
  if (memberStore.profile) {
    const res = await GetNoteTypeAPI({
      Auth: memberStore.profile.Token,
      UserGroup: memberStore.profile.UserGroup,
    })
    res.result.push('ALL')
    noteTypes.value = res.result
  }
}
//选择账户类型
const pickerNoteTypeChange = async (e: any) => {
  const index = e.detail.value as number
  curNoteType.value = noteTypes.value![index]
  await onRefresherrefresh()
}
</script>
<template>
  <scroll-view
    enable-back-to-top
    scroll-y
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefresherrefresh"
  >
    <view class="viewport">
      <view class="header" :style="{ paddingTop: safeAreaInsets!.top + 'px' }">
        <view class="left icon-left" @tap="Onleft()"></view>
        <view class="condition">
          <picker @change="pickerNoteTypeChange" mode="selector" :range="noteTypes">
            <text class="account">{{ curNoteType }}</text></picker
          >
        </view>
        <uni-title type="h1" align="center" title="随心记" class="text"></uni-title>
        <view class="btn right" @tap="Add">新增</view>
      </view>
      <scroll-view enable-back-to-top scroll-y class="scroll">
        <view class="detail" v-for="(item, index) in data" :key="index">
          <uni-list
            v-if="!(userSetting?.SoftStyle == '简约' && item.UserTitle.indexOf('年计划') == 0)"
          >
            <uni-list-item
              link
              :title="item.UserTitle"
              :note="item.UserName"
              :rightText="item.CreateTime"
              :to="`./NoteLog?ID=` + item.ID"
              :thumb="item.IsColl ? 'http://120.48.90.254:8080/Uploads/shoucang.png' : ''"
              thumb-size="sm"
              clickable
            >
            </uni-list-item>
          </uni-list>
        </view>
      </scroll-view>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './styles/NoteLogMain.scss';
</style>
